package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.bussness.domain.BidRequest;
import cn.wolfcode.p2p.bussness.query.BidRequestQueryObject;
import cn.wolfcode.p2p.bussness.service.IBidRequestAuditHistoryService;
import cn.wolfcode.p2p.bussness.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class BidRequestController {

    @Autowired
    private IBidRequestService bidRequestService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IRealAuthService realAuthService;

    @Autowired
    private IBidRequestAuditHistoryService bidRequestAuditHistoryService;

    @RequestMapping("/bidrequest_publishaudit_list")
    public String bidRequestPage(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
            qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPLY);
            model.addAttribute("pageInfo", bidRequestService.queryList(qo));
        return "bidrequest/publish_audit";
    }

    @RequestMapping("/bidrequest_publishaudit")
    @ResponseBody
    public AjaxResult publishAudit(Long id, Integer state, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            Date publishTime, String remark) {
        AjaxResult result = null;
        try {
            bidRequestService.publishAudit(id, state, publishTime, remark);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }

    @RequestMapping("/borrow_info")
    public String borrowInfo(Long id, Model model) {
        BidRequest bidRequest = bidRequestService.get(id);
        //返回页面
        String returnPage = "";
        //判断当前传过来的ID 为不为空
        if (bidRequest != null) {
            //bidRequest 如果不为null
            model.addAttribute("bidRequest", bidRequest);

            //如果当前借款对象 == 信用标的话
            if(bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_NORMAL){
                //userInfo 用户信息
                UserInfo userInfo = userInfoService.get(bidRequest.getCreateUser().getId());
                model.addAttribute("userInfo", userInfo);
                //realAuth 真实信息
                model.addAttribute("realAuth", realAuthService.get(userInfo.getRealAuthId()));
                //audits
                model.addAttribute("audits", bidRequestAuditHistoryService.queryListByBidRequestId(bidRequest.getId()));

                returnPage = "bidrequest/borrow_info";
            }else if(bidRequest.getBidRequestType() == Constants.BIDREQUEST_TYPE_EXP){
                //audits
                model.addAttribute("audits", bidRequestAuditHistoryService.queryListByBidRequestId(bidRequest.getId()));
                returnPage =  "expbidrequest/borrow_info";
            }
        }
        return returnPage;
    }

    @RequestMapping("/bidrequest_audit1_list")
    public String audit1Page(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
       //设置成满标一审 控制界面显示的只有满标一审数据
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_1);
        //pageInfo
        model.addAttribute("pageInfo", bidRequestService.queryList(qo));
        return "bidrequest/audit1";
    }

    @RequestMapping("/bidrequest_audit1")
    @ResponseBody
    public AjaxResult audit1(Long id, Integer state, String remark) {
        AjaxResult result = null;
        try {
            bidRequestService.audit1(id,state,remark);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }

    @RequestMapping("/bidrequest_audit2_list")
    public String audit2Page(@ModelAttribute("qo") BidRequestQueryObject qo, Model model) {
       //设置成满标一审 控制界面显示的只有满标二审数据
        qo.setBidRequestState(Constants.BIDREQUEST_STATE_APPROVE_PENDING_2);
        //pageInfo
        model.addAttribute("pageInfo", bidRequestService.queryList(qo));
        return "bidrequest/audit2";
    }

    @RequestMapping("/bidrequest_audit2")
    @ResponseBody
    public AjaxResult audit2(Long id, Integer state, String remark) {
        AjaxResult result = null;
        try {
            bidRequestService.audit2(id,state,remark);
            result = new AjaxResult();
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
